package xyz.scootaloo.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author flutterdash@qq.com
 * @since 2021/4/21 17:52
 */
public class IOTool {

    public static List<String> readFileAsList(String filename) {
        String realpath = getRealPath(filename);
        List<String> res = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(realpath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                res.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    private static String getRealPath(String filename) {
        return Objects.requireNonNull(
                getLoader().getResource(filename)).getFile();
    }

    private static ClassLoader getLoader() {
        return IOTool.class.getClassLoader();
    }

}
